package com.lz.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <pre>
 * @描述：创建动态代理的工具
 * @作者：lizhao
 * @创建时间： 2024/9/26 下午3:14
 * </pre>
 **/
public class ProxyUtil {
    public static Star createStar(Jostar jostar) {

        return (Star) Proxy.newProxyInstance(ProxyUtil.class.getClassLoader(), new Class[]{Star.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equals("sing")) {
                    System.out.println("Jostar to singing");
                }
                if (method.getName().equals("dance")) {
                    System.out.println("Jostar to dance");
                }
                return method.invoke(jostar, args);
            }
        });
    }
}
